package com.l4.software.pixie.kernel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import java.awt.Toolkit;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * DeskPixie启动窗口(程序入口)
 * @author 李骞
 * 
 */
public class Launcher extends JFrame{

	
	private static final long serialVersionUID = 1L;

	private Dimension framesize=new Dimension(400,240); //窗口尺寸 
	private Toolkit toolkit;
	private Image backgroundImage; //背景图片
	private JProgressBar jpb; //进度条
	private JPanel jPanel;
	private Main main;	
	
	private int task_amout = 3;	//设置初始化任务数量
	
	
	// 设定图像显示状态(设定文字边缘平滑)
	private final static RenderingHints hints;
	static {		
		hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	}
	
	
	//Constructor
	public Launcher() {
		super("DeskPixie");
		this.setLayout(new BorderLayout());

		
		//添加窗口关闭事件，触发系统退出
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}			
		});
		
		/*程序需要等进度条加载完毕再继续
		//添加鼠标单击事件，出发系统退出
		this.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}			
		});
		*/
		
		//初始化进度条
		jpb=new JProgressBar(SwingConstants.HORIZONTAL,0,task_amout);
		jpb.setIndeterminate(false);
		jpb.setValue(0);
		jpb.setString("初始化中...");
		jpb.setFont(new Font("宋体",Font.PLAIN,12));
		jpb.setStringPainted(true);
		this.add(jpb, BorderLayout.SOUTH);
		jpb.setVisible(true);
		
		//初始化JPanel
		jPanel=new JPanel(){

			private static final long serialVersionUID = 1L;

			@Override
			public void paint(Graphics g) {		
				//super.paint(g); //重写此方法不需要调用super类的paint方法
				
				Graphics2D graphics2D=(Graphics2D)g; //获取图形上下文
				graphics2D.setRenderingHints(hints); //设定显示设置
				
				//载入图像并显示
				MediaTracker mt=new MediaTracker(this);		
				backgroundImage=toolkit.createImage("back.jpg");
				mt.addImage(backgroundImage, 0);
				try {
					mt.waitForAll();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				
				if(mt.checkID(0)==true)
					graphics2D.drawImage(backgroundImage,0,0,null);
				
				//绘制文字
				graphics2D.setFont(new Font("Comic Sans MS",Font.BOLD,36));
				graphics2D.setColor(Color.WHITE);
				graphics2D.drawString("DeskPixie", 110, 40);
				graphics2D.setColor(Color.RED);
				graphics2D.drawString("DeskPixie", 109, 39);
			}

		};
		jPanel.setSize(framesize);
		this.add(jPanel);
		
		
		//获取屏幕尺寸，从而设置窗口显示居中
		toolkit=Toolkit.getDefaultToolkit();
		Dimension screensize=toolkit.getScreenSize();//屏幕尺寸
		this.setSize(framesize);
		this.setLocation((screensize.width-framesize.width)/2, (screensize.height-framesize.height)/2);
		
		this.setUndecorated(true); //无标题栏(无Border)窗口
		this.setVisible(true); //显示窗口
		
		initialize();//调用初始化方法
	}
	
	/**
	 * 初始化主程序
	 * @author 李骞
	 */
	private void initialize() {
		try {
			task1();		
			//task2();
			//task3();			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		dispose(); //释放Splash
		main.open();

	}


	//初始化任务	
	/**
	 * 初始化所要执行的任务
	 * @throws InterruptedException
	 */
	private void task1() throws InterruptedException {
		main=new Main(this);
		main.initialize();
		jpb.setMaximum(main.getModuleAmout()+1); //最大值=用户组件数量+1(Kernel)
	}

	
	//设置滚动条状态
	public synchronized void setState(String state){
		jpb.setString(state);
		jpb.setValue(jpb.getValue()+1);
	}
	
	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		new Launcher();		
	}	
}

 
